import 'package:dio/dio.dart';
import 'http_service.dart';

/// GitCode API 服务类
/// 封装 GitCode OpenAPI 的调用
class GitCodeApiService {
  final HttpService _httpService;
  
  GitCodeApiService(this._httpService);
  
  /// 获取当前用户信息
  Future<Map<String, dynamic>> getCurrentUser({CancelToken? cancelToken}) async {
    try {
      final response = await _httpService.get(
        '/user',
        cancelToken: cancelToken,
      );
      return response.data as Map<String, dynamic>;
    } catch (e) {
      rethrow;
    }
  }
  
  /// 获取用户信息（通过用户名）
  Future<Map<String, dynamic>> getUser(String username, {CancelToken? cancelToken}) async {
    try {
      final response = await _httpService.get(
        '/users/$username',
        cancelToken: cancelToken,
      );
      return response.data as Map<String, dynamic>;
    } catch (e) {
      rethrow;
    }
  }
  
  /// 获取用户的仓库列表
  Future<List<dynamic>> getUserRepositories({
    String? username,
    int page = 1,
    int perPage = 20,
    CancelToken? cancelToken,
  }) async {
    try {
      final path = username != null ? '/users/$username/repos' : '/user/repos';
      final response = await _httpService.get(
        path,
        queryParameters: {
          'page': page,
          'per_page': perPage,
        },
        cancelToken: cancelToken,
      );
      return response.data as List<dynamic>;
    } catch (e) {
      rethrow;
    }
  }
  
  /// 获取仓库信息
  Future<Map<String, dynamic>> getRepository({
    required String owner,
    required String repo,
    CancelToken? cancelToken,
  }) async {
    try {
      final response = await _httpService.get(
        '/repos/$owner/$repo',
        cancelToken: cancelToken,
      );
      return response.data as Map<String, dynamic>;
    } catch (e) {
      rethrow;
    }
  }
  
  /// 搜索仓库
  Future<Map<String, dynamic>> searchRepositories({
    required String query,
    int page = 1,
    int perPage = 20,
    CancelToken? cancelToken,
  }) async {
    try {
      final response = await _httpService.get(
        '/search/repositories',
        queryParameters: {
          'q': query,
          'page': page,
          'per_page': perPage,
        },
        cancelToken: cancelToken,
      );
      return response.data as Map<String, dynamic>;
    } catch (e) {
      rethrow;
    }
  }
  
  /// 获取仓库的 Issues
  Future<List<dynamic>> getRepositoryIssues({
    required String owner,
    required String repo,
    String? state, // open, closed, all
    int page = 1,
    int perPage = 20,
    CancelToken? cancelToken,
  }) async {
    try {
      final response = await _httpService.get(
        '/repos/$owner/$repo/issues',
        queryParameters: {
          if (state != null) 'state': state,
          'page': page,
          'per_page': perPage,
        },
        cancelToken: cancelToken,
      );
      return response.data as List<dynamic>;
    } catch (e) {
      rethrow;
    }
  }
  
  /// 获取仓库的 Pull Requests
  Future<List<dynamic>> getRepositoryPullRequests({
    required String owner,
    required String repo,
    String? state, // open, closed, all
    int page = 1,
    int perPage = 20,
    CancelToken? cancelToken,
  }) async {
    try {
      final response = await _httpService.get(
        '/repos/$owner/$repo/pulls',
        queryParameters: {
          if (state != null) 'state': state,
          'page': page,
          'per_page': perPage,
        },
        cancelToken: cancelToken,
      );
      return response.data as List<dynamic>;
    } catch (e) {
      rethrow;
    }
  }
  
  /// 获取仓库的 Commits
  Future<List<dynamic>> getRepositoryCommits({
    required String owner,
    required String repo,
    String? sha, // branch or commit SHA
    int page = 1,
    int perPage = 20,
    CancelToken? cancelToken,
  }) async {
    try {
      final response = await _httpService.get(
        '/repos/$owner/$repo/commits',
        queryParameters: {
          if (sha != null) 'sha': sha,
          'page': page,
          'per_page': perPage,
        },
        cancelToken: cancelToken,
      );
      return response.data as List<dynamic>;
    } catch (e) {
      rethrow;
    }
  }
  
  /// 获取仓库的 Branches
  Future<List<dynamic>> getRepositoryBranches({
    required String owner,
    required String repo,
    CancelToken? cancelToken,
  }) async {
    try {
      final response = await _httpService.get(
        '/repos/$owner/$repo/branches',
        cancelToken: cancelToken,
      );
      return response.data as List<dynamic>;
    } catch (e) {
      rethrow;
    }
  }
  
  /// 获取仓库的 Tags
  Future<List<dynamic>> getRepositoryTags({
    required String owner,
    required String repo,
    CancelToken? cancelToken,
  }) async {
    try {
      final response = await _httpService.get(
        '/repos/$owner/$repo/tags',
        cancelToken: cancelToken,
      );
      return response.data as List<dynamic>;
    } catch (e) {
      rethrow;
    }
  }
  
  /// 创建 Issue
  Future<Map<String, dynamic>> createIssue({
    required String owner,
    required String repo,
    required String title,
    String? body,
    List<String>? labels,
    CancelToken? cancelToken,
  }) async {
    try {
      final response = await _httpService.post(
        '/repos/$owner/$repo/issues',
        data: {
          'title': title,
          if (body != null) 'body': body,
          if (labels != null) 'labels': labels,
        },
        cancelToken: cancelToken,
      );
      return response.data as Map<String, dynamic>;
    } catch (e) {
      rethrow;
    }
  }
}

